import json
import re
from django import http
from django.shortcuts import render
# Create your views here.
from django.views import View
from apps.users.models import User

# 用户实名认证
from rent_house.utils.qiniuyun import img_upload


class RealNameView(View):
    # POST api/v1.0/user/auth
    def get(self, request):
        user = request.user
        if not user.is_authenticated:
            return http.JsonResponse({'errno': 4101, 'errmsg': '用户没有实名认证'})
        if not user.id_card:
            return http.JsonResponse({'errno': 0, 'errmsg': 'ok', 'data':  {
            'real_name': None,
            'id_card':None,
        }})
        id_card = user.id_card[0:5] + '*' * 8 + user.id_card[-5:-1]
        data = {
            'real_name': user.real_name,
            'id_card': id_card,
        }
        return http.JsonResponse({'errno': 0, 'errmsg': 'ok', 'data': data})

    def post(self,request):
        json_dict = json.loads(request.body.decode())
        real_name = json_dict.get('real_name')  # 用户真实姓名
        id_card = json_dict.get('id_card')  #用户身份证号码

        if not all([real_name,id_card]):
            return http.JsonResponse({'errno':'4103','errmsg':'缺少必传参数'})

        if not re.match(r'([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})',real_name):
            # 1、不能有特殊字符和数字；
            # 2、可以输入英文，可以有空格，可以输入英文名字中的点；
            # 3、可以输入汉字；
            # 4、中文英文不能同时出现；
            # 5、长度在1-20；
            return http.JsonResponse({"errno": "4103", "errmsg": "参数real_name错误"})

        if not re.match(r'^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$',id_card):
            return http.JsonResponse({"errno": "4103", "errmsg": "参数id_card错误"})

        try:
            users = User.objects.get(id=request.user.id)
        except:
            return http.JsonResponse({"errno": "4002", "errmsg": "查询无该用户"})
        count =User.objects.filter(id_card=id_card).count()
        if count:
            return http.JsonResponse({"errno": "4003", "errmsg": "该身份证号已被使用"})
        users.real_name=real_name
        users.id_card = id_card
        users.save()
        data ={
            'real_name': real_name, 'id_card': id_card
        }
        return http.JsonResponse({"errno": "0", "errmsg": "认证信息保存成功",'data':data})

# 用户名修改
class UpdateNameView(View):
    # PUT api/v1.0/users/name/
    def put(self, request):
        json_dict = json.loads(request.body.decode())
        name = json_dict.get('name')  #用户名

        if not all([name]):
            return http.JsonResponse({"errno": "4103", "errmsg": "缺少必穿参数"})
        try:
            users = User.objects.get(id=request.user.id)
        except:
            return http.JsonResponse({"errno": "4002", "errmsg": "查询无该用户"})
        users.first_name = name
        users.username = name
        users.save()
        return http.JsonResponse({"errno": "0", "errmsg": "修改成功"})

# 上传个人头像
class UploadPhoto(View):
    # POST api/v1.0/users/avatar
    def post(self, request):
        user = request.user
        avatar = request.FILES.get('avatar')  # 头像文件
        if not all([avatar]):
            return http.JsonResponse({"errno": "4103", "errmsg": "缺少必穿参数"})
        data = avatar.read()
        img_url = img_upload(data)
        user.avatar = img_url
        user.save()
        data = {
            "avatar_url": img_url
        }
        return http.JsonResponse({'data': data, 'errno': "0", 'errmsg': "头像上传成功"})


# 个人中心
class MyCenterView(View):
    # GET api/v1.0/users/
    def get(self, request):
        try:
            users = User.objects.get(id=request.user.id)
        except:
            return http.JsonResponse({"errno": "4002", "errmsg": "查询无该用户"})
        try:
            avatar_url = users.avatar.url
        except:
            avatar_url = 'http://qjkq2sdyz.hn-bkt.clouddn.com/FgeIOLVVP3K-nMyONdjkAIeKKRt5'
        data = {
            "avatar_url":avatar_url,  # 用户头像地址
            "create_time": users.last_login,  # 用户创建时间
            "mobile": users.mobile,  # 用户手机号
            "name":users.first_name  if users.first_name else users.mobile,  # 用户昵称
            "user_id": request.user.id  # 用户id
        }
        return http.JsonResponse({'data': data, 'errmsg': 'ok', 'errno': 0})
